<script type="text/javascript">

    function createContactPanel(){
        var cPanel;
        if (!cPanel) {
            cForm = Ext.create('Ext.form.Panel', {
                action:'submit',
                defaults:{
                    anchor:'100%'
                },
                bodyPadding:10,
                items:[

                    {
                        xtype:'combobox',
                        fieldLabel:'Contact',
                        store:contactsStore,
                        queryMode:'local',
                        displayField:'value',
                        valueField:'key',
                        name:'contact',
                        grow: true
                    }

                ]
            });
            cPanel = Ext.create('Ext.window.Window', {
                title:'Add contact',
                width:500,
                height:125,
                layout:'fit',
                items:cForm,
                closeAction:'hide',
                buttons:[
                    {
                        text:'Add',
                        handler:function () {
                            var mailValue = mailForm.getForm().findField('email').getValue();
                            if(mailValue=='')
                                mailForm.getForm().findField('email').setValue(cForm.getForm().findField('contact').getValue());
                            else{
                                mailForm.getForm().findField('email').setValue(mailValue + ', '+cForm.getForm().findField('contact').getValue());
                            }

                            cPanel.hide();

                        }
                    },
                    {
                        text:'Close',
                        handler:function () {

                            cPanel.hide();
                        }
                    }
                ]
            });
        }
        cPanel.show();
        cPanel.center();
    }
    
    function createMailPanel() {
        var mailPanel;
        if (!mailPanel) {
           
           var wh = $('#warehouse').val();
            var whText = $('#warehouse :selected').text();
    submitUrl = 'index.php?r=report/inventory/Warehouse/mail/1/wid/' + wh+'/options/'+Ext.JSON.encode(options);
    
            mailForm = Ext.create('Ext.form.Panel', {
                action:'submit',
                defaults:{
                    anchor:'100%'
                },
                bodyPadding:10,
                items:[
                    {
                        xtype:'box',
                        autoEl:{
                            tag:'a',
                            href:'javascript:void(0)',
                            onclick:'createContactPanel()',
                            html:'Add Contact '
                        }
                    },
                    {
                        fieldLabel:'Email',
                        xtype:'textfield',
                        name:'email',
                        allowBlank:false
                    },
                    {
                        fieldLabel:'Subject',
                        xtype:'textfield',
                        name:'subject',

                        allowBlank:false,
                        value:'Warehouse Report for  : ' +whText
                    },

                    {

                        xtype:'htmleditor',
                        enableColors:false,
                        enableAlignments:false,
                        name:'message',
                        fieldLabel:'Message',
                        anchor:'100%'
                    }
                ]
            });

            mailPanel = Ext.create('Ext.window.Window', {
                title:'Mail Form',
                width:600,
                height:400,
                layout:'fit',
                items:mailForm,
                closeAction:'hide',
                buttons:[
                    {
                        text:'Submit',
                        handler:function () {

					
									showOkDialog('CSV generation can take a while');
                            mailForm.submit({

                                url:submitUrl,

                                success:function (form, action) {

									showOkDialog(action.result.msg);
                                    mailPanel.close();
                                },
                                failure:function (form, action) {
                                    Ext.Msg.alert('Failed', action.result.msg);
                                }

                            });
                            mailPanel.hide();

                        }
                    },
                    {
                        text:'Close',
                        handler:function () {

                            mailPanel.hide();
                        }
                    }
                ]
            });
        }
        mailPanel.show();
        mailPanel.center();
    }
    $(document).ready(function(){
    options = {};
      options['zero'] = $('#zero').attr('checked')?true:false;
     options['cost'] = $('#cost').attr('checked')?true:false;
     options['reorder'] = $('#reorder').attr('checked')?true:false;
    $('#reportForm').change(function(){
     options['zero'] = $('#zero').attr('checked')?true:false;
     options['cost'] = $('#cost').attr('checked')?true:false;
     options['reorder'] = $('#reorder').attr('checked')?true:false;
    });
    });

function getHTML() {
        var wh = $('#warehouse').val();
        
        var url = 'index.php?r=report/inventory/Warehouse/wid/' + wh+'/html/1/options/'+Ext.JSON.encode(options);
            console.log(url);
   
       window.open(url);
    }
    function getCSV() {

        var wh = $('#warehouse').val();
        var url = 'index.php?r=report/inventory/Warehouse/wid/' + wh+'/options/'+Ext.JSON.encode(options);
        
        window.open(url);
    }
    Ext.onReady(function () {
        contactsStore = Ext.create('Ext.data.Store', {
            model:'Combomodel',
            data:contacts
        });


    });

</script>
<?php
$this->breadcrumbs = array(
    'Report',
);?>
<h1>Warehouse Report</h1>

<div class="form">

    <?php
    echo CHtml::form('', 'post', array('id' => 'reportForm'));
    ?>


    <div class="row">
        <?php echo CHtml::label('Warehouse', 'Warehouse'); ?>
        <?php echo CHtml::dropDownList('Warehouse','', Warehouse::getList(),array('id'=>'warehouse')) ?>
    </div>
    <div class="row">
         <?php echo CHtml::label('Include Zero QTY parts', 'zero'); ?>
        <?php echo CHtml::checkBox('zero',false,array('id'=>'zero')); ?>
    </div>
      <div class="row">
         <?php echo CHtml::label('Include average cost', 'cost'); ?>
        <?php echo CHtml::checkBox('cost',false,array('id'=>'cost')); ?>
    </div>

      <div class="row">
         <?php echo CHtml::label('Show Reorder Levels ', 'reorder'); ?>
        <?php echo CHtml::checkBox('cost',false,array('id'=>'reorder')); ?>
    </div>

        <?php echo CHtml::link('View HTML report', '#', array('id' => 'reportHTML',
        'onclick' => 'javascript:getHTML();')) ?>
         <br/>
            <?php echo CHtml::link('Download .csv report', '#', array('id' => 'reportHTML',
        'onclick' => 'javascript:getCSV();')) ?>
          <br/>
        <?php echo CHtml::link('Send to Email .csv report', '#', array('id' => 'mailPdf',
        'onclick' => 'javascript:createMailPanel();')) ?>
 
    <?
    echo CHtml::endForm();
    ?>

</div>
